package com.okx.bot.component;

import com.okx.bot.service.OkxService;
import com.okx.bot.service.SendService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Slf4j
@Component
public class OkxTaskRunner implements CommandLineRunner {
    private final OkxService okxService;
    private final SendService sendService;
    private List<String> instIdList = new ArrayList<>();
    @Value("${okx.volume-threshold}")
    private double volumeThreshold;

    public OkxTaskRunner(OkxService okxService, SendService sendService) {
        this.okxService = okxService;
        this.sendService = sendService;
    }

    @Override
    public void run(String... args) {
        getEnableInstIds();
    }

    @Scheduled(cron = "0 30 8 * * ?")
    public void getEnableInstIds() {
        log.info("开启getEnableInstIds定时任务");
        if (instIdList.size() != 0) {
            instIdList.clear();
        }
        log.info("获取交易对");
        List<String> instIds = okxService.getAllProd();
        log.info("总共获取到instIds：" + instIds.size());
        long startTime = System.currentTimeMillis();
        // 获取日线符合的交易对
        List<String> highVolumeContracts = okxService.getHighVolumeContracts(instIds, volumeThreshold);
        long endTime = System.currentTimeMillis();
        long executionTime = endTime - startTime;
        log.info(" 筛选交易对时间为：" + executionTime + " 毫秒");
        // 4. 输出结果到控制台
        log.info("符合条件的合约交易对:");
        highVolumeContracts.forEach(h -> {
            instIdList.add(h);
        });
        System.out.println(instIdList);
    }

    @Scheduled(fixedRate = 30000)
    public void getEnableTrade() {
        if (instIdList.size() > 0) {
            List<String> strings = okxService.executeVolStrategy(instIdList);
            strings.stream().forEach(s -> {
                sendService.wxSend(s);
            });
        }

    }
}
